
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class Query 
{
    public List getFileFromTFile()
    {
        Session session = HibernateUtil.currentSession();
        Transaction tx = session.beginTransaction();
        String query = "SELECT file FROM TFile file";
        List list = session.createQuery(query).list();
        session.flush();
        session.getTransaction().commit();
        HibernateUtil.closeSession();
        return list;
    }
    public void clearTable(String table_name)
    {
        Session session = HibernateUtil.currentSession();
        Transaction tx = session.beginTransaction();
        String query = "DELETE FROM " + table_name;
        session.createQuery(query).executeUpdate();
        session.flush();
        session.getTransaction().commit();
        HibernateUtil.closeSession();
    }
    //--For fill ComboBox----------------------------------------------------------------------------------------
    public List getElementsFromTFileForColumn(String column_name)
    {
        Session session = HibernateUtil.currentSession();
        Transaction tx = session.beginTransaction();
        String query = "SELECT file." + column_name + " FROM TFile file";
        List list = session.createQuery(query).list();
        session.flush();
        session.getTransaction().commit();
        HibernateUtil.closeSession();
        ArrayList<String> different_item_list = getDifferentItemsFromList(list);
        return different_item_list;  
    }
    private ArrayList getDifferentItemsFromList(List list)
    {
        ArrayList<String> different_item_list = new ArrayList<String>();
        Iterator list_iterator = list.iterator();
        while(list_iterator.hasNext())
        {
           String current_item = list_iterator.next().toString();
           if(different_item_list.contains(current_item) == false)
               different_item_list.add(current_item);            
        }
        return different_item_list;  
    }
    //--For Absciss Data---------------------------------------------------------------------------------------
    public List getSizesList(String type, String language)
    {
        Session session = HibernateUtil.currentSession();
        Transaction tx = session.beginTransaction();
        String query = "SELECT file.size " +
                       "FROM TFile file " +
                       "WHERE file.type =:type_ " +
                       "AND file.language =:language_ " +
                       "ORDER BY file.size";
        List list = session.createQuery(query).setParameter("type_", type)
                                              .setParameter("language_", language).list();
        session.flush();
        session.getTransaction().commit();
        HibernateUtil.closeSession();
//        ArrayList<String> different_item_list = getDifferentItemsFromList(list);
//        return different_item_list;
        return list;
    }
    public List getTypesList(Long size, String language)
    {
        Session session = HibernateUtil.currentSession();
        Transaction tx = session.beginTransaction();
        String query = "SELECT file.type " +
                       "FROM TFile file " +
                       "WHERE file.size =:size_ " +
                       "AND file.language =:language_";
        List list = session.createQuery(query).setParameter("size_", size)
                                              .setParameter("language_", language).list();
        session.flush();
        session.getTransaction().commit();
        HibernateUtil.closeSession();
//        ArrayList<String> different_item_list = getDifferentItemsFromList(list);
//        return different_item_list;
        return list;
    }
    public List getLanguagesList(Long size, String type)
    {
        Session session = HibernateUtil.currentSession();
        Transaction tx = session.beginTransaction();
        String query = "SELECT file.language " +
                       "FROM TFile file " +
                       "WHERE file.size =:size_ " +
                       "AND file.type =:type_";
        List list = session.createQuery(query).setParameter("size_", size)
                                              .setParameter("type_", type).list();
        session.flush();
        session.getTransaction().commit();
        HibernateUtil.closeSession();
//        ArrayList<String> different_item_list = getDifferentItemsFromList(list);
//        return different_item_list;
        return list;
    }
    //--For ordinate data For File--------------------------------------------------------------------------
    public List getDataFromTArchiveFileForColumn(String column_name, Long size, String type, String language, 
                                                 String compression_level, String compressor)
    {
        Session session = HibernateUtil.currentSession();
        Transaction tx = session.beginTransaction();
        String query = "SELECT arch_file." + column_name + " " +
                       "FROM TArchiveFile arch_file " +
                       "INNER JOIN arch_file.file as file " +
                       "WHERE arch_file.compression_level =:compression_level_ " +
                       "AND arch_file.compressor =:compressor_ " + 
                       "AND file.size =:size_ " + 
                       "AND file.type =:type_ " +
                       "AND file.language =:language_";
        List list = session.createQuery(query).setParameter("compression_level_", compression_level)
                                              .setParameter("compressor_", compressor)
                                              .setParameter("size_", size)
                                              .setParameter("type_", type)
                                              .setParameter("language_", language).list();
        session.flush();
        session.getTransaction().commit();
        HibernateUtil.closeSession();
        return list;
    }
    
    //--For ordinate data For Crypto File------------------------------------------------------------------------- 
    public List getDataFromTArchiveCryptoFileForColumn(String column_name, Long size, String type, String language, 
                                                       String compressor, String crypto_algorithm, String encrypt_mode)
    {
        Session session = HibernateUtil.currentSession();
        Transaction tx = session.beginTransaction();
        String query = "SELECT arch_crypto_file." + column_name + " " +
                       "FROM TArchiveCryptoFile arch_crypto_file " +
                       "INNER JOIN arch_crypto_file.crypto_file as crypto_file " +
                       "INNER JOIN crypto_file.file as file " +
                       "WHERE arch_crypto_file.compressor =:compressor_ " +
                       "AND crypto_file.algorithm =:algorithm_ " + 
                       "AND crypto_file.mode =:mode_ " +
                       "AND file.size =:size_ " + 
                       "AND file.type =:type_ " +
                       "AND file.language =:language_";
        List list = session.createQuery(query).setParameter("compressor_", compressor)
                                              .setParameter("algorithm_", crypto_algorithm)
                                              .setParameter("mode_", encrypt_mode)
                                              .setParameter("size_", size)
                                              .setParameter("type_", type)
                                              .setParameter("language_", language).list();
        session.flush();
        session.getTransaction().commit();
        HibernateUtil.closeSession();
        return list;
    }
}
